package com.hustzp.com.xichuangzhu.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hustzp.com.xichuangzhu.R;
import com.hustzp.com.xichuangzhu.model.Collections;
import com.hustzp.com.xichuangzhu.utils.ImageUtils;
import com.hustzp.com.xichuangzhu.widget.CircleImageView;

import java.util.List;

/**
 * Copyright (C) 2015 The huimai mobile client Project
 * All right reserved.
 *
 * @author: wuzhimin@huimai365.com
 * @date: 2016/5/28 0:06
 * @Description:
 */
public class CollectionAdapter extends BaseAdapter {
    private Context context;
    private List<Collections> collectionses;

    public CollectionAdapter(Context context, List<Collections> collectionses) {
        this.context = context;
        this.collectionses = collectionses;
    }

    @Override
    public int getCount() {
        return collectionses.size();
    }

    @Override
    public Collections getItem(int position) {
        return collectionses.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context,
                    R.layout.fragment_category_child_item, null);
            holder.initView(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        bindViews(position, holder);
        return convertView;
    }

    private void bindViews(int position, ViewHolder holder) {
        Collections collections = getItem(position);
        holder.tv_category_title.setText(collections.getName());
        ImageUtils.loadImage(collections.getCover(), holder.imgPic);
    }

    class ViewHolder {
        CircleImageView imgPic;
        TextView tv_category_title;

        public void initView(View view) {
            tv_category_title = (TextView) view
                    .findViewById(R.id.tvName);

            imgPic = (CircleImageView) view.findViewById(R.id.imgPic);
        }
    }
}
